package xyz.bali16.module.person.information.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.starblues.bootstrap.annotation.AutowiredType;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.bali16.application.entity.User;
import xyz.bali16.application.mapper.UserMapper;
import xyz.bali16.module.person.information.entity.UserInformation;
import xyz.bali16.module.person.information.mapper.UserInformationMapper;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;


@Service
@AllArgsConstructor
public class UserInformationServiceImpl extends ServiceImpl<UserInformationMapper, UserInformation> implements UserInformationService {
    //@Autowired
    //@AutowiredType(AutowiredType.Type.PLUGIN_MAIN)
    //private final UserMapper userMapper;

    @Override
    public void add(String userId, String informationId) {
        if (informationId == null) {
            return;
        }
        //List<UserInformation> userInformations = informationIds.stream()
        //        .filter(informationId -> !StrUtil.isEmpty(informationId))
        //        .map(informationId -> {
        UserInformation userInformation = new UserInformation();
        userInformation.setUserId(userId);
        userInformation.setInformationId(informationId);

        //})
        //.collect(Collectors.toList());

        save(userInformation);
    }

    @Override
    public void removeByInformationId(String informationId) {
        if (StrUtil.isEmpty(informationId)) {
            return;
        }
        Wrapper<UserInformation> wrapper = Wrappers.<UserInformation>lambdaQuery()
                .eq(UserInformation::getInformationId, informationId);
        remove(wrapper);
    }

    @Override
    public void removeByUserId(String userId) {
        if (StrUtil.isEmpty(userId)) {
            return;
        }
        Wrapper<UserInformation> wrapper = Wrappers.<UserInformation>lambdaQuery()
                .eq(UserInformation::getUserId, userId);
        remove(wrapper);
    }

    @Override
    public void removeByInformationId(String userId,String informationId) {
        if (StrUtil.isEmpty(userId)) {
            return;
        }
        if (informationId == null) {
            return;
        }
        Wrapper<UserInformation> wrapper = Wrappers.<UserInformation>lambdaQuery()
                .eq(UserInformation::getUserId, userId)
                .eq(UserInformation::getInformationId, informationId);
        remove(wrapper);
    }

    @Override
    public UserInformation getByUserId(String userId) {
        Wrapper<UserInformation> wrapper = Wrappers.<UserInformation>lambdaQuery()
                .eq(UserInformation::getUserId, userId);
        return getOne(wrapper);
    }

    @Override
    public List<UserInformation> getByInformationId(String informationId) {
        if (StrUtil.isEmpty(informationId)) {
            return Collections.emptyList();
        }
        Wrapper<UserInformation> wrapper = Wrappers.<UserInformation>lambdaQuery()
                .eq(UserInformation::getInformationId, informationId);
        return list(wrapper);
    }


}
